<template>
	<view>
		<navbar></navbar>
		<view class="container" v-if="isShow">
			<view class="container-title"></view>
			<!-- <view class="Top_tilte">
				修改地址
			</view> -->
			<view class="item" v-for="(item,index) in addressList" :key="index">
				<view class="top">
					<view class="name">
						{{item.receiver}}
					</view>
					<view class="phone">
						{{item.receiver_phone}}
					</view>
				</view>
				<view class="address">
					<view class="text twoOver">
						{{item.format_detail}}
					</view>
					<!-- <view class="icon" @click="nav_Data(item.address_id)">
						<img src="@/static/images/user/address.png" alt="">
					</view> -->
				</view>
				<view class="operation">
					<view class="switch" @click="isSwitch(item)">
						<image  v-if="item.is_default == 1"  src="/static/images/xuanzhonga.png" mode="widthFix"></image>
						<image  v-else src="/static/images/xuanzhong.png" mode="widthFix"></image>
						<!-- <switch :checked="item.is_default == 1 ? true:false" :disabled="item.is_default == 1 ? true:false"  style="margin-left: 30rpx;" @change="isSwitch" @click="ismodify(item,index)"/> -->
						设为默认
					</view>
					<view class="delete" @click="Delete(item.address_id)">
						删除
					</view>
				</view>
			</view>
			<view class="item" @click="nav_add">
				<view class="item-btn">
					添加地址
				</view>
			</view>
		
		</view>
		<view class="lack" v-else>
			<view class="yoshop-notcont" style="padding: 0;">
				<text class="iconfont icon-wushuju" style="color: #636363;"></text>
			</view>
			<view class="title">
				您暂时没有添加的地址
			</view>
		</view>
		<view class="bottom">
			<view class="btn" @click="submit">
				提交
			</view>
		</view>
	</view>
</template>

<script>
	const app = getApp();
	export default {
		data() {
			return {
				addressList:[],//
				isShow:false,
				checked:false,//按钮状态
				order_id:0,//订单id
				address_id:0,//地址id
			};
		},
		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad(options) {
			this.order_id = options.order_id
		this.getAddress()
		},
		onPullDownRefresh() { 
			
		},
		methods: {
			submit(){
				let _this = this
				this.addressList.forEach((item)=>{
					if(item.is_default == 1){
						_this.address_id = item.address_id
					}
				})
				uni.showModal({
					content: `收货地址确定后不可修改，是否确定提交？`,
					success: (res) => {
						if (res.confirm) {
							try {
								app._post_form('Jadegoods/addJadeLuckyOrderAddress', {
									order_id:_this.order_id,
									address_id:_this.address_id
								}, function(result) {
									if (1 == result.code) {
										app.showToast('选择成功');
										setTimeout(() => {
											uni.navigateBack({
												delta:1
											})
										}, 1500)
										
									}
									
								}, null, function() {
									// 解除按钮禁用
									app.showError(result.msg);
								});
							
							} catch (e) {
								// Do something when catch error
							}
						}
					}
				});
			},
			//获取地址列表
			getAddress(){
				let _this = this
				app._post_form('JadeAddress/lists', {}, function(result) {
					if (1 == result.code) {

						
						if(result.data.list.length > 0){
							_this.isShow = true
							_this.addressList = result.data.list
						}else{
							_this.isShow = false
						}
						
					}
				}, null, function() {
					// 解除按钮禁用
					app.showError(result.msg);
				});
			},
			//删除地址
			Delete(e){
				let _this = this
				app._post_form('JadeAddress/delete', {
					address_id:e
				}, function(result) {
					if (1 == result.code) {
				    app.showToast(result.msg);
					_this.getAddress()
						
					}
				}, null, function() {
					// 解除按钮禁用
					app.showError(result.msg);
				});
			},
			//修改默认地址
			Modify(e){
				let _this = this
				app._post_form('JadeAddress/setDefault', {
					address_id:e
				}, function(result) {
					if (1 == result.code) {
				    app.showToast(result.msg);
					_this.getAddress()
						
					}
				}, null, function() {
					// 解除按钮禁用
					app.showError(result.msg);
				});
			},
			//添加
			nav_add(){
				console.log("11111")
				uni.redirectTo({
					url: `/pages/user/addressManagement/add`
				})
			},
			//修改
			nav_Data(e){
				uni.redirectTo({
					url: `./addData?address_id=${e}`
				})
			},
			//修改默认
			isSwitch(e){
				
				console.log(e,'地址的id')
				if(e.is_default == 1){
					
				}else{
					this.addressList.map(item=>{
						item.is_default = 0
					})
					e.is_default = 1
					
					this.Modify(e.address_id)
				}
				// this.checked = e.detail.value
				// if(e.detail.value){
				// 	this.addressList.forEach((item)=>{
				// 		item.is_default = 0
				// 	})
				// 	e.is_default = 1
				// }else{
				// 	e.is_default = 1
				// 	   app.showToast('不能取消默认地址哦~');
				// }
				this.$forceUpdate()
			},
			ismodify(e,i){
				console.log(e,i)
				let _this = this
				if(e.is_default == 1){
					 app.showToast('不能取消默认地址哦~');
				}else{
					_this.addressList.forEach((item,index)=>{
						if(index == i){
							item.is_default = 1
							_this.Modify(item.address_id)
						}else{
							item.is_default = 0
						}
							
					})
				}
				
			}
		}
	}
</script>

<style lang="scss">
	page{
		background-color: #000001;
	}
	.lack{
		width: 100%;
		height: 100vh;
		display: flex;
		flex-direction: column;
		    justify-content: center;
		    align-items: center;
			letter-spacing: 2rpx;
			
			.icon{
				width: 300rpx;
				height: 300rpx;
				text{
					width: 100%;
					height: 100%;
				}
			}
			.title{
				font-size: 40rpx;
				font-weight: bold;
				color: #333333;
			}
	}
	.container-title{
		margin-top: 100rpx;
		width: 100%;
		height: 30rpx;
		background: url(@/static/images/orderTitle.png);
		background-size: 100% 100%;
	}
	.Top_tilte {
		margin-top: 40rpx;
		width: 100%;
		height: 80rpx;
		background-color: #FFFFFF;
		text-align: center;
		color: rgba(0, 0, 0, 0.9);
		font-size: 30rpx;
		font-family: 'dy-iconfont';
		font-weight: bold;
		line-height: 80rpx;
	}
	.container{
		padding-bottom: 300rpx;
		
		.item{
			margin: 32rpx;
			box-sizing: border-box;
			// height: 342rpx;
			padding: 24rpx;
			background-color: #171819;
			border-radius: 10rpx;
			
			.top{
				display: flex;
				font-size: 30rpx;
				color: #FFFFFF;
				margin-bottom: 30rpx;
				.name{
					margin-right: 10rpx;
				}
			}
			.address{
				padding-bottom: 10rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;
				border-bottom: 1px solid rgba(51,51,51,0.0600);
				.text{
					width: 542rpx;
					// height: 120rpx;
					font-size: 26rpx;
					color: #A8A8A8;
				}
				.icon{
					width: 48rpx;
					height: 48rpx;
					img{
						width: 100%;
						height: 100%;
					}
				}
			}
			.operation{
				margin-top: 15rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;
				.delete{
					font-size: 28rpx;
					font-weight: 500;
					color: #FF602E;
				}
			}
			
			
		}
	}
	.bottom{
		position: fixed;
		bottom: 60rpx;
		left: 0;
		width: 100%;
		box-shadow: 0rpx -1rpx 8rpx 0rpx rgba(0,0,0,0.0600);
		.btn{
			margin: 16rpx 28rpx;
			height: 106rpx;
			text-align: center;
			background: linear-gradient(#D8B255 100%,#FFF4D7 100%);
			font-size: 30rpx;
			color: #000001;
			display: flex;
			align-items: center;
			justify-content: center;
			border-radius: 92rpx;
		}
	}
	.item-btn{
		height: 126rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		font-size: 30rpx;
		color: #FFFFFF;
	}
	.switch{
		font-size: 26rpx;
		color: #9E9E9F;
		display: flex;
		align-items: center;
	}
	.switch image{
		width:32rpx;
		height: 32rpx;
		margin-right: 10rpx;
	}
</style>
